<?php

function getRatio($filename){

    list($lebar_asli, $tinggi_asli) = getimagesize($filename);
        
    if($lebar_asli>$tinggi_asli){
        $data['ratio'] = $tinggi_asli/$lebar_asli;
        $data['orientation'] = 'L';
    }else{
        $data['ratio'] = $lebar_asli/$tinggi_asli;
        $data['orientation'] = 'P';
    }
    
    return $data;
}

function newCustomImg($filename, $ratio, $orient, $max){
    
    list($lebar_asli, $tinggi_asli, $imageType) = getimagesize($filename);

    $imageType = image_type_to_mime_type($imageType);
    switch($imageType) {
      case "image/gif":
        $current_image=imagecreatefromgif($filename); 
        break;
        case "image/pjpeg":
      case "image/jpeg":
      case "image/jpg":
        $current_image=imagecreatefromjpeg($filename); 
        break;
        case "image/png":
      case "image/x-png":
        $current_image=imagecreatefrompng($filename); 
        break;
    }

    switch ($orient) {
      case 'P':
        $newheight = $max;
        $newwidth = ceil($newheight*$ratio);
        break;
      case 'L':
        $newwidth = $max;
        $newheight = ceil($newwidth*$ratio);
        break;
    }

    switch ($orient) {
      case 'P':
          $xheight = 480;
          $xwidth = ceil(($xheight*$newwidth)/$newheight);
        break;
      case 'L':
          if($ratio<0.5){
            $xwidth = 960;
            $xheight = ceil(($xwidth*$newheight)/$newwidth);
          }else{
            $xheight = 480;
            $xwidth = ceil(($xheight*$newwidth)/$newheight);
          }
        break;
    }

    

    $data['height'] = $xheight;
    $data['width'] = $xwidth;

    // $current_image = imagecreatefromjpeg($filename);
    $data['canvas'] = imagecreatetruecolor($newwidth, $newheight);
    
    imagecopyresampled($data['canvas'], $current_image, 0, 0, 0, 0, $newwidth, $newheight, $lebar_asli, $tinggi_asli);

    return $data;
}

function resizeThumbnailImage($image, $width, $height, $start_width, $start_height, $w, $h, $thumb){
  list($imagewidth, $imageheight, $imageType) = getimagesize($image);
  $imageType = image_type_to_mime_type($imageType);

  $width = ceil(($imagewidth/$w)*$width);
  $height = ceil(($imageheight/$h)*$height);
  
  // $scale = $thumb/$width;

  $start_width = $start_width * ($imagewidth / $w);
  $start_height = $start_height * ($imageheight / $h);

  // $newImageWidth = ceil($width * $scale);
  // $newImageHeight = ceil($height * $scale);

  $newImageWidth = $thumb;
  $newImageHeight = $thumb;
  $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
  switch($imageType) {
    case "image/gif":
      $source=imagecreatefromgif($image); 
      break;
      case "image/pjpeg":
    case "image/jpeg":
    case "image/jpg":
      $source=imagecreatefromjpeg($image); 
      break;
      case "image/png":
    case "image/x-png":
      $source=imagecreatefrompng($image); 
      break;
    }
  imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);
  return $newImage;
  // chmod($thumb_image_name, 0777);
  // return $thumb_image_name;
    // return $start_width."|".$start_height."|".$newImageWidth."|".$newImageHeight."|".$width."|".$height;
}

?>